const { app, BrowserWindow, ipcMain } = require("electron")
const path = require("path")
const fs = require("fs")

const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        autoHideMenuBar: true,
        // 预加载js 
        webPreferences: {
           preload: path.resolve(__dirname, './preload.js')
        }
    })
    ipcMain.on('save-file',writeFile)
    ipcMain.handle('read-file',readFile)
    win.loadFile('./pages/index.html')
}

const writeFile = (event, data) => {
    fs.writeFileSync('D:/mygo.txt', data)
}
const readFile = () => {
    return fs.readFileSync('D:/mygo.txt').toString()
}
app.on("ready", () => {
    createWindow()
    // mac 激活窗口
    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
})
// 当所有的窗口都关闭时 如果不是mac 就退出
app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
        app.quit()
    }
})

